class Solution {
public:
    vector<vector<int>> rst;
    vector<int> temp;

    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());

        for(int i = 0; i <= nums.size(); ++i){
            subsetsStep(nums, 0, i);
        }

        return rst;
    }

    void subsetsStep(vector<int>& nums, int start, int size) {
        if(temp.size() == size){
            rst.push_back(temp);
            return;
        }

        for(int i = start; i < nums.size() - (size - temp.size()) + 1; ++i) {
            if(i != start && nums[i] == nums[i - 1]) {
                continue;
            }

            temp.push_back(nums[i]);
            subsetsStep(nums, i + 1, size);
            temp.pop_back();
        }
    }
};